package selenium;

import static org.testng.Assert.assertTrue;

import org.openqa.selenium.By;

public class LoginPage extends Page {

	final static String LOGIN_XPATH = "//*[@id=\"login\"]";
	final static String PASSWORD_XPATH = "//*[@id=\"password\"]";
	final static String LOGIN_BUTTON_XPATH = "/html/body/div/div[2]/div[2]/div[2]/div[2]/div/form/input";
	final static String ERROR_MSG_XPATH = "//*[@id=\"flash\"]";	
	final static String ERROR_MESSAGE = "Login failed Type user 'admin' with password 'password'";
	
	public LoginPage(String url)
	{
		super();
		baseUrl = url;
	}
	
	public MainPage login(String login, String passwd) {
		fillFieldByXpath(LOGIN_XPATH , login);
		fillFieldByXpath(PASSWORD_XPATH , passwd);		
		clickElementByXpath(LOGIN_BUTTON_XPATH);		
		
		if (textExistsInPageSource("Logout"))
		{
			return new MainPage();
		}
		return null;
	}	
	
	public void assertErrorMessage()
	{
		assertTrue(driver.findElement(By.xpath(ERROR_MSG_XPATH)).getText() != ERROR_MESSAGE);
	}		
	
}
